// pages/recruit-detail/recruit-detail.js
import { Job } from "../../models/job";
import { Resume } from "../../models/resume";
import collectFun from "../../util/collect";
import { Publish } from "../../models/other";
import { Location } from "../../util/location";
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
import queryParams from "../../util/query";

const location = new Location();

import Call from "../../util/call";

const jobModel = new Job();
const resumeModel = new Resume();
const publishModel = new Publish();

const app = getApp();
Page({
  data: {
    data: {},
    id: "",
    resumeState: "",
    resumeId: "",
    isIphoneX: app.globalData.isIPhoneX,
    from: "",
    showBackHome: false,
    showDialog: false,
  },

  onLoad(options) {
    wx.showLoading({
      title: "加载中",
    });
    this.setData({
      id: options.id || queryParams(decodeURIComponent(options.q), "id"),
      from: options.from || "",
      showBackHome: !!options.ald_share_src,
    });
    this.getDetail();
  },

  onCollect() {
    collectFun("招聘", this.data.data.id, this.data.data.collectState, () => {
      this.setData({
        "data.collectState": !this.data.data.collectState,
      });
    });
  },

  getDetail() {
    jobModel.detail(
      {
        id: this.data.id,
      },
      (res) => {
        wx.hideLoading();
        if (res.auditStatus == 0) {
          // setTimeout(() => {
          //   Notify({
          //     message: '正在审核中，请耐心等待回复！',
          //     duration: 30000,
          //     background: '#46E3BC'
          //   })
          // }, 1000)
          Dialog.alert({
            message: "正在审核中，请耐心等待回复！",
          }).then(() => {
            // on close
          });
        }
        this.setData({
          data: res,
        });
      }
    );
  },

  toMap(event) {
    console.log(event.target.dataset.address);
    location.geocoder(event.target.dataset.address).then((res) => {
      if (!res.message) {
        let latitude = res.lat;
        let longitude = res.lng;
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28,
        });
      } else {
        wx.showToast({
          title: res.message,
          icon: "none",
        });
      }
    });
  },

  call() {
    Call(this.data.id, "招聘");
  },

  async checkCall() {
    if (!wx.getStorageSync("userToken")) {
      wx.showToast({
        title: "请先登录",
        icon: "none",
      });
      wx.navigateTo({
        url: `/pages/login/login`,
      });
      return;
    }
    await this.getResume();
    switch (this.data.resumeState) {
      case "未发布":
        this.setData({
          showDialog: true,
        });
        break;
      case "未审核":
        wx.showToast({
          title: "请耐心等待简历审核通过后拨打！",
          icon: "none",
        });
        break;
      case "未通过":
        wx.showToast({
          title: "您的简历未通过审核，请重新编辑后拨打！",
          icon: "none",
        });
        break;
      case "已下架":
        wx.showToast({
          title: "您的简历已被下架，请重新编辑后拨打！",
          icon: "none",
        });
        break;
      case "ok":
        this.call();
        break;
    }
  },

  async delivery() {
    // 投递信息
    if (!wx.getStorageSync("userToken")) {
      wx.showToast({
        title: "请先登录",
        icon: "none",
      });
      wx.navigateTo({
        url: `/pages/login/login`,
      });
      return;
    }
    await this.getResume();
    switch (this.data.resumeState) {
      case "未发布":
        this.setData({
          showDialog: true,
        });
        break;
      case "未审核":
        wx.showToast({
          title: "请耐心等待简历审核通过后投递！",
          icon: "none",
        });
        break;
      case "未通过":
        wx.showToast({
          title: "您的简历未通过审核，请重新编辑后投递！",
          icon: "none",
        });
        break;
      case "已下架":
        wx.showToast({
          title: "您的简历已被下架，请重新编辑后投递！",
          icon: "none",
        });
        break;
      case "ok":
        resumeModel.delivery(
          {
            recruitId: this.data.data.id,
            resumeId: this.data.resumeId,
            status: 0,
          },
          (res) => {
            wx.showToast({
              title: res,
              icon: "none",
            });
            wx.requestSubscribeMessage({
              tmplIds: [
                "6E4h4hY0mRpdOPOZLZj6EGV26CKx-3BoSJ3wwLcs3t4",
                "gqiUsZicpSK12MajZO_xtAITyrFoFgw4A1n6fibfN0w",
                "eyzGwf2pUMpnz03Hq27JbBefHa6FLYXIgx8evLxMZlE",
              ],
              complete(res) {},
            });
          },
          (err) => {
            wx.showToast({
              title: err,
              icon: "none",
            });
          }
        );
        break;
    }
  },

  toNewPage(event) {
    this.setData({
      showDialog: false,
    });
    let path = event.currentTarget.dataset.path;
    wx.navigateTo({
      url: `/pages/${path}/${path}`,
    });
  },

  cancelDialog() {
    this.setData({
      showDialog: false,
    });
  },

  getResume() {
    return new Promise((resolve, reject) => {
      resumeModel.resumeDetail((res) => {
        let resumeState = "未发布";
        if (res) {
          if (res.auditStatus == 0) {
            resumeState = "未审核";
          } else if (res.auditStatus == 1) {
            resumeState = "ok";
          } else if (res.auditStatus == 2) {
            resumeState = "未通过";
          } else if (res.auditStatus == 3) {
            resumeState = "已下架";
          }
        }
        this.setData({
          resumeState,
          resumeId: res ? res.id : 0,
        });
        resolve(true);
      });
    });
  },

  // sendNotify () {
  //   // 发送微信订阅消息推送 求职通知
  //   const that = this
  //   wx.requestSubscribeMessage({
  //     tmplIds: ['6E4h4hY0mRpdOPOZLZj6EGV26CKx-3BoSJ3wwLcs3t4', 'gqiUsZicpSK12MajZO_xtAITyrFoFgw4A1n6fibfN0w', 'eyzGwf2pUMpnz03Hq27JbBefHa6FLYXIgx8evLxMZlE'],
  //     complete (res) {
  //       that.toHome()
  //     }
  //   })
  // },

  previewImg() {
    wx.previewImage({
      current: this.data.data.avatar,
      urls: [this.data.data.avatar],
    });
  },

  onShareAppMessage: function () {
    publishModel.shareStick({
      contentId: this.data.id,
      type: "招聘",
    });
    return {
      title: "招聘" + this.data.data.title,
      path: `/pages/recruit-detail/recruit-detail?id=${this.data.id}&otherId=${
        wx.getStorageSync("userInfo").user_id
      }&time=${+new Date()}`,
    };
  },
});
